package com.zc.im.controller;

import com.alibaba.fastjson.JSONObject;
import com.zc.im.model.im.group.ZcImGroupLogModel;
import com.zc.im.model.im.group.ZcImGroupLog;
import com.zc.im.service.im.group.ImGroupLogService;
import com.zc.im.util.JsonUtils;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.security.interfaces.RSAKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 日志controller
 * Created by wangcc on 2017/7/8.
 */
@Controller
@RequestMapping(value = "/grouplog")
public class ImGroupLogController {

    @Autowired
    private ImGroupLogService imGroupLogService;
    private Logger logger = Logger.getLogger(ImGroupLogController.class);

    /**
     * 添加日志的方法
     * @param insertGroupLog
     * @return ResponseJson
     */
    @RequestMapping(value = "/insertGroupLog",method = RequestMethod.POST)
   public ResponseJson insertGroupLog(String insertGroupLog) {
       ResponseJson responseJson = new ResponseJson();
       if (StringUtils.isEmpty(insertGroupLog)) {
           return responseJson;
       }
       ZcImGroupLog zcImGroupLog = JsonUtils.strToClass(insertGroupLog, ZcImGroupLog.class);
       Integer key = null;
       try {
       key = imGroupLogService.saveGroupLog( zcImGroupLog );
       } catch (Exception e) {
        logger.error(e.getMessage(),e);
       }
       Map<String,Object> dataMap = new HashMap<>();
       dataMap.put("key",key);
       responseJson.setDataMap(dataMap);
       return responseJson;
   }

    /**
     * 根据群id 和管理员id查找日志
     * 传入的json}{"Id":"群id","adminUserId":"管理员id"}
     * @return
     */
    @RequestMapping(value = "/queryGroupLog",method =RequestMethod.POST)
  public ResponseJson queryGroupLog(String queryGroupLog) {
        ResponseJson responseJson = new ResponseJson();
        if (queryGroupLog==null) {
            return responseJson;
        }
        JSONObject jsonType = JsonUtils.strToJsonObj(queryGroupLog);
        List<ZcImGroupLog> ZcImGroupLog = imGroupLogService
        .queryGroupLog(Integer.parseInt(jsonType.getString("Id")),Integer.parseInt(jsonType
        .getString("adminUserId")));
        Map<String , Object> dataMap = new HashMap<>();
        dataMap.put("key",JsonUtils.objToStr( ZcImGroupLog ));
        responseJson.setDataMap(dataMap);
        return  responseJson;
  }

    /**
     * 查日志所有信息评分，评论人，评论内容，用户头像， 评论人头像，名称
     * 传入的json{"LogId":""}
     * @param querGroupLogSocreMark
     * @return
     */
    @RequestMapping(value = "/queryGroupLogSocreMark",method = RequestMethod.POST)
  public ResponseJson queryGroupLogSocreMark(String querGroupLogSocreMark) {
      ResponseJson responseJson = new ResponseJson();
      if (StringUtils.isEmpty(querGroupLogSocreMark)) {
          return  responseJson;
      }
     JSONObject jsonType = JsonUtils.strToJsonObj(querGroupLogSocreMark);
      List<ZcImGroupLogModel> listZcImGroupLogModel = imGroupLogService.queyGroupLogSocreMark(Integer.parseInt(jsonType
      .getString("logId")));
      Map<String , Object > dataMap = new HashMap<>();
      dataMap.put("key", JsonUtils.objToStr(listZcImGroupLogModel));
      responseJson.setDataMap(dataMap);
      return responseJson;
  }
    /**
     *通过周月年检索出时间
     * 传入的json("id":"查看者id","groupId":"群时间","startTime":"开始时间","endTime","结束时间")
     * @param queryTimeGroupLog
     * @return
     */
  @RequestMapping(value = "/queryTimeGroupLog",method =RequestMethod.POST)
  public ResponseJson queryTimeGroupLog(String queryTimeGroupLog) {
        ResponseJson responseJson = new ResponseJson();
        if (StringUtils.isEmpty(queryTimeGroupLog)) {
            return responseJson;
        }
        JSONObject jsonObject = JsonUtils.strToJsonObj(queryTimeGroupLog);
        String id = jsonObject.getString("id");
        String groupId = jsonObject.getString("groupId");
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        List<ZcImGroupLog> listZcImGroupLog = imGroupLogService.queryTimeGroupId(Integer.parseInt(id),Integer.parseInt(groupId),startTime,endTime);
        Map<String ,Object> dataMap = new HashMap<>();
        dataMap.put("key",JsonUtils.objToStr(listZcImGroupLog));
        responseJson.setDataMap(dataMap);
        return responseJson;


  }
}
